package com.roncoo.education.course.service.admin.biz;

import com.roncoo.education.common.service.BaseBiz;
import com.roncoo.education.common.core.base.Page;
import com.roncoo.education.common.core.base.PageUtil;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.core.tools.BeanUtil;
import com.roncoo.education.course.service.admin.req.AdminExperimentScoreStepEditReq;
import com.roncoo.education.course.service.admin.req.AdminExperimentScoreStepPageReq;
import com.roncoo.education.course.service.admin.req.AdminExperimentScoreStepSaveReq;
import com.roncoo.education.course.service.admin.resp.AdminExperimentScoreStepPageResp;
import com.roncoo.education.course.service.admin.resp.AdminExperimentScoreStepViewResp;
import com.roncoo.education.course.dao.ExperimentScoreStepDao;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreStep;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreStepExample;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreStepExample.Criteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;

/**
 * ADMIN-实验步骤分数
 *
 * @author wujing
 */
@Component
@RequiredArgsConstructor
public class AdminExperimentScoreStepBiz extends BaseBiz {

    @NotNull
    private final ExperimentScoreStepDao dao;

    /**
     * 实验步骤分数分页
     *
     * @param req 实验步骤分数分页查询参数
     * @return 实验步骤分数分页查询结果
     */
    public Result<Page<AdminExperimentScoreStepPageResp>> page(AdminExperimentScoreStepPageReq req) {
        ExperimentScoreStepExample example = new ExperimentScoreStepExample();
        Criteria c = example.createCriteria();
        Page<ExperimentScoreStep> page = dao.page(req.getPageCurrent(), req.getPageSize(), example);
        Page<AdminExperimentScoreStepPageResp> respPage = PageUtil.transform(page, AdminExperimentScoreStepPageResp.class);
        return Result.success(respPage);
    }

    /**
     * 实验步骤分数添加
     *
     * @param req 实验步骤分数
     * @return 添加结果
     */
    public Result<String> save(AdminExperimentScoreStepSaveReq req) {
        ExperimentScoreStep record = BeanUtil.copyProperties(req, ExperimentScoreStep.class);
        if (dao.save(record) > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }

    /**
     * 实验步骤分数查看
     *
     * @param id 主键ID
     * @return 实验步骤分数
     */
    public Result<AdminExperimentScoreStepViewResp> view(Long id) {
        return Result.success(BeanUtil.copyProperties(dao.getById(id), AdminExperimentScoreStepViewResp.class));
    }

    /**
     * 实验步骤分数修改
     *
     * @param req 实验步骤分数修改对象
     * @return 修改结果
     */
    public Result<String> edit(AdminExperimentScoreStepEditReq req) {
        ExperimentScoreStep record = BeanUtil.copyProperties(req, ExperimentScoreStep.class);
        if (dao.updateById(record) > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }

    /**
     * 实验步骤分数删除
     *
     * @param id ID主键
     * @return 删除结果
     */
    public Result<String> delete(Long id) {
        if (dao.deleteById(id) > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }
}
